using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using DAL;
using DTO;

namespace WellmeadowsHospital
{
    public partial class frmAllocated : DevExpress.XtraEditors.XtraUserControl
    {
        StaffBLL staffBLL = new StaffBLL();
        StaffOfWardBLL sowBLL = new StaffOfWardBLL();
        PositionBLL positionBLL = new PositionBLL();
        SProcBLL SPBLL = new SProcBLL();
        JobBLL jobBLL = new JobBLL();
        WardBLL wardBLL = new WardBLL();
        private string position;
        public frmAllocated()
        {
            InitializeComponent();
            BindingData();
            BindingDataWard();
            //lay ve mot PositionDTO chua cac position in Hospital
            foreach (PositionDTO item in positionBLL.SelectAll())
            {
                cbbPostitionAllocate.Properties.Items.Add(item.Position);
            }
        }
        public void BindingDataSearch()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = staffBLL.SearchIDName(txtSearch.Text);
            navStaff.BindingSource = bs;
            gcStaffListAllocated.DataSource = bs;
            

        }
        public void BindingData()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = staffBLL.SelectNewStaff();
            navStaff.BindingSource = bs;
            gcStaffListAllocated.DataSource = bs;
        }
        public void BindingDataWard()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = wardBLL.SelectAllWard();
            navWard.BindingSource = bs;
            gcWardListAllocated.DataSource = bs;
        }
        public void BindingDataStaffInWard(string wardid)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = sowBLL.ListStaffInWard(wardid);
            navStaffWard.BindingSource = bs;
            gcStaffOfWardT.DataSource = bs;
        }
        

        
        private void gvWardListAllocated_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtAllocateAllocatedT.Text = gvWardListAllocated.GetRowCellValue(gvWardListAllocated.GetSelectedRows()[0], gcWardID).ToString();
            gcStaffOfWardT.DataSource = sowBLL.ListStaffInWard(txtAllocateAllocatedT.Text);
      
        }
        //cap phat job cho cac staff in ward
        private void btnSaveAllocateT_Click(object sender, EventArgs e)
        {
            if (SPBLL.KiemtraPositionSample(cbbPostitionAllocate.SelectedItem.ToString(), txtStaffIDAllocateT.Text, txtAllocateAllocatedT.Text))
            {
                MessageBox.Show("Same position of staff work in ward");
            }
            else
            {
                if (cbbPostitionAllocate.SelectedIndex != -1)
                {


                    if (cbbPostitionAllocate.SelectedItem.Equals(position))
                    {
                        if (gvNewStaffAllocated.GetRowCellValue(gvNewStaffAllocated.GetSelectedRows()[0], gcStatus).ToString().Equals("Work"))
                        {
                            if (!txtAllocateAllocatedT.Text.Equals(""))
                            {
                                StaffOfWardDTO sw = new StaffOfWardDTO();
                                sw.StaffID = txtStaffIDAllocateT.Text;
                                sw.WardId = txtAllocateAllocatedT.Text;
                                sw.StartDate = deStartDateT.DateTime;
                                if (sowBLL.Insert(sw).Equals("success"))
                                {
                                    MessageBox.Show("Insert Successful");
                                }
                                else
                                {
                                    MessageBox.Show(sowBLL.Insert(sw));
                                }
                            }
                            gcStaffListAllocated.DataSource = staffBLL.SelectNewStaff();
                            gcStaffOfWardT.DataSource = sowBLL.ListStaffInWard(txtAllocateAllocatedT.Text);
                        }
                    }
                    else
                    {
                        JobDTO job = new JobDTO();
                        job.StaffId = txtStaffIDAllocateT.Text;
                        foreach (PositionDTO item in positionBLL.SelectAll())
                        {
                            if (cbbPostitionAllocate.SelectedItem.ToString().Equals(item.Position))
                            {
                                job.Position = item.PositionId;
                            }
                        }
                        job.CurrentSalary = Convert.ToInt32(txtCurrentSalaryT.Text);
                        job.SalaryScale = txtSalaryScaleT.Text;
                        job.Hours = float.Parse(txtHoursT.Text);
                        job.Paid = cbbPaidT.SelectedItem.ToString();
                        job.Contract = cbbPTT.SelectedItem.ToString();
                        job.StartDate = deStartDateT.DateTime;

                        if (jobBLL.Insert(job).Equals("success"))
                        {
                            if (!txtAllocateAllocatedT.Text.Equals(""))
                            {
                                StaffOfWardDTO sw = new StaffOfWardDTO();
                                sw.StaffID = txtStaffIDAllocateT.Text;
                                sw.WardId = txtAllocateAllocatedT.Text;
                                sw.StartDate = deStartDateT.DateTime;
                                if (sowBLL.Insert(sw).Equals("success"))
                                {
                                    MessageBox.Show("Insert Successful");
                                }
                                else
                                {
                                    MessageBox.Show(sowBLL.Insert(sw));
                                }
                            }
                            gcStaffListAllocated.DataSource = staffBLL.SelectNewStaff();
                            gcStaffOfWardT.DataSource = sowBLL.ListStaffInWard(txtAllocateAllocatedT.Text);
                        }
                        else
                        {
                            MessageBox.Show(jobBLL.Insert(job));
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please choose postion");
                }
            }


            txtSearch.Text = "";
        }
        public void ResetAllocated()
        {
            txtStaffIDAllocateT.Text = "";
            txtAllocateAllocatedT.Text = "";
            cbbPostitionAllocate.EditValue = "";
            cbbPostitionAllocate.EditValue = "Select Position";
            txtHoursT.Text = "";
            txtCurrentSalaryT.Text = "";
            txtSalaryScaleT.Text = "";
            cbbPaidT.EditValue = "W";
            cbbPTT.EditValue = "P";
            deStartDateT.EditValue = "";
        }
        private void btnResetAllocateT_Click(object sender, EventArgs e)
        {
            ResetAllocated();
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            BindingDataSearch();
        }

        private void gvWardListAllocated_RowClick_1(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtAllocateAllocatedT.Text = gvWardListAllocated.GetRowCellValue(gvWardListAllocated.GetSelectedRows()[0], gcWardID).ToString();
            BindingDataStaffInWard(gvWardListAllocated.GetRowCellValue(gvWardListAllocated.GetSelectedRows()[0], gcWardID).ToString());
            
        }

        private void gvNewStaffAllocated_RowClick_1(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtAllocateAllocatedT.Enabled = true;
            txtStaffIDAllocateT.Text = gvNewStaffAllocated.GetRowCellValue(gvNewStaffAllocated.GetSelectedRows()[0], gcStaffID).ToString();
            if (gvNewStaffAllocated.GetRowCellValue(gvNewStaffAllocated.GetSelectedRows()[0], gcStatus).ToString().Equals("Work"))
            {
                List<JobDTO> list = staffBLL.ReceiveJob(gvNewStaffAllocated.GetRowCellValue(gvNewStaffAllocated.GetSelectedRows()[0], gcStaffID).ToString());
                //lay WardID tuong ung voi job cua nhan vien
                List<string> li = staffBLL.ReceiveWard(gvNewStaffAllocated.GetRowCellValue(gvNewStaffAllocated.GetSelectedRows()[0], gcStaffID).ToString());
                string item = "";
                foreach (string a in li)
                {
                    item = a;
                }
                foreach (JobDTO job in list)
                {
                    txtAllocateAllocatedT.Text = item;
                    gcStaffOfWardT.DataSource = sowBLL.ListStaffInWard(txtAllocateAllocatedT.Text);
                    cbbPostitionAllocate.SelectedItem = job.PositionName;
                    position = job.PositionName;
                    txtHoursT.Text = job.Hours.ToString();
                    txtCurrentSalaryT.Text = job.CurrentSalary.ToString();
                    txtSalaryScaleT.Text = job.SalaryScale;
                    cbbPaidT.SelectedItem = job.Paid;
                    cbbPTT.SelectedItem = job.Contract;
                    deStartDateT.DateTime = job.StartDate;
                    deFinishDateAllocate.Enabled = true;
                }
            }

        }
    }
}
